package org.gary.base.action;

import java.io.File;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.gary.base.model.Image;
import org.gary.base.service.ImageService;
import org.gary.core.util.Utils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 资源action
 * @author lengreen
 * 
 */
@Component
@Scope("prototype")
public class UploadAction extends ActionSupport implements ServletResponseAware {
	private static final long serialVersionUID = 1L;
	
	private HttpServletResponse response;
	
	/** 产品的service **/
	private ImageService imageService;
	/** 用于规定上传文件的大小 **/
	private static final int BUFFER_SIZE = 200 * 1024;
	/** 上传的文件 **/
	private File filedata;
	/** 上传文件真实名字 **/
	private String filedataFileName;
	/** 文件类型 **/
	private String filedataContentType;
	/** 文件保存路径 **/
	private String savePath;
	/** 下载文件路径型 **/
	private Image image;
	/**返回结果字符串*/
	private String result;

	/**
	 * 上传文件
	 * @throws Exception
	 */
	public void upload() throws Exception {
		if (getFiledata() != null) {
			HttpServletRequest request = ServletActionContext.getRequest();
			String fileName = UUID.randomUUID().toString()
					+ getFiledataFileName().substring(
							getFiledataFileName().lastIndexOf("."));
			setSavePath("descImage");
			File dst = new File(request.getSession().getServletContext()
					.getRealPath(getSavePath())
					+ "\\" + fileName);
			if (Utils.copy(getFiledata(), dst, BUFFER_SIZE)) {
				image = new Image();
				image.setName(fileName);
				image.setPath(request.getContextPath() + "/" + getSavePath()
						+ "/" + fileName);
				imageService.save(image);
				result = "{'success':'true','msg':'" + image.getPath() + "'}";
			}
		} else {
			result = "{'success':'true','msg':'ok'}";
		}
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(result);
	}

	public File getFiledata() {
		return filedata;
	}

	public void setFiledata(File filedata) {
		this.filedata = filedata;
	}

	public String getFiledataFileName() {
		return filedataFileName;
	}

	public void setFiledataFileName(String filedataFileName) {
		this.filedataFileName = filedataFileName;
	}

	public String getFiledataContentType() {
		return filedataContentType;
	}

	public void setFiledataContentType(String filedataContentType) {
		this.filedataContentType = filedataContentType;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	@Resource
	public void setImageService(ImageService imageService) {
		this.imageService = imageService;
	}

	public ImageService getImageService() {
		return imageService;
	}

	public void setImage(Image image) {
		this.image = image;
	}

	public Image getImage() {
		return image;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

}